<template>
  <div class="app-container org">
    <el-card class="leftCard">
      <el-row>
        <el-col :span="4">
          <p class="pinformation" style="margin-top: 8.5px">批量导出</p>
        </el-col>
        <el-col :span="16">
          <el-form-item label="核查日期：">
            <el-date-picker
              value-format="YYYY-MM-DD"
              type="daterange"
              v-model="dateRange3"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-button
            link
            icon="TrendCharts"
            style="line-height: 2; padding-left: 10px"
            >批量导出</el-button
          >
        </el-col>
      </el-row>

      <!-- 搜索条件盒子 -->
      <el-row>
        <el-col :span="15">
          <p class="pinformation">搜索条件</p>
        </el-col>
      </el-row>

      <el-row>
        <el-form :model="queryParams" ref="queryRef" label-width="105">
          <el-row>
            <el-col :span="9">
              <el-form-item label="标准物质名称：" prop="bzwzName">
                <el-input
                  clearable
                  maxlength="100"
                  v-model="queryParams.bzwzName"
                  placeholder="请输入标准物质名称"
                  @keyup.enter="handleQuery"
                />
              </el-form-item>
            </el-col>

            <el-col :span="9">
              <el-form-item label="唯一编号：" prop="bzwzCode" label-width="75">
                <el-input
                  clearable
                  maxlength="100"
                  v-model="queryParams.bzwzCode"
                  placeholder="请输入唯一编号"
                  @keyup.enter="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-row justify="end">
                <el-button type="success" @click.enter="handleQuery"
                  >搜索</el-button
                >
                <el-button class="resetBtn" @click="resetQuery">重置</el-button>
              </el-row>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="9">
              <el-form-item label="批次号：" prop="produceCode">
                <el-input
                  clearable
                  maxlength="100"
                  v-model="queryParams.produceCode"
                  placeholder="请输入批次号"
                  @keyup.enter="handleQuery"
                />
              </el-form-item>
            </el-col>

            <el-col :span="9">
              <el-form-item label="归属类别：" prop="gsType" label-width="75">
                <el-select
                  placeholder="请选择"
                  clearable
                  v-model="queryParams.gsType"
                  filterable
                >
                  <el-option
                    v-for="dict in proxy.addListNoLimit(sys_bzwz_gslb)"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <!-- <el-form-item label="在库情况：" prop="" label-width="75">
                <el-select
                  placeholder="请选择"
                  clearable
                  v-model="form.gsType"
                  filterable
                >
                  <el-option
                    v-for="dict in sys_bzwz_gslb"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                  />
                </el-select>
              </el-form-item> -->
            </el-col>
          </el-row>
        </el-form>
      </el-row>
      <div>
        <el-row>
          <p class="pinformation">标准物质列表</p>
          <p class="totalStyle">共 {{ total }} 条数据</p>
        </el-row>
        <el-table
          ref="mainTableRef"
          class="business"
          v-loading="loading"
          :data="bzwzTable"
          :row-class-name="tableRowClass"
          @row-click="rowClick"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            label="标准物质名称"
            align="center"
            prop="bzwzName"
            width="230"
            show-overflow-tooltip
          />
          <el-table-column label="唯一编号" align="center" prop="bzwzCode" />
          <el-table-column label="批次号" align="center" prop="produceCode" />
          <!-- 字段 -->
          <el-table-column
            label="上次核查日期"
            align="center"
            prop="checkDate"
            width="110"
          />
          <el-table-column label="在库情况" align="center" prop="remainGg" />
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </el-card>

    <el-card class="rightCard">
      <!-- 搜索条件区域 -->
      <el-row>
        <el-col :span="8">
          <el-form-item label="标准物质名称：" prop="bzwzName">
            {{ searchOutinForm.bzwzName }}
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="唯一编号：" prop="bzwzCode">
            {{ searchOutinForm.bzwzCode }}
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="批次号：" prop="produceCode">
            {{ searchOutinForm.produceCode }}
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="2">
          <p class="pinformation">搜索条件</p>
        </el-col>
      </el-row>
      <el-form
        label-width="120px"
        :model="searchOutinForm"
        ref="searchOutinRef"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="状态：" prop="stepId">
              <el-select
                v-model="searchOutinForm.stepId"
                placeholder="请选择记录状态"
                clearable
                style="width: 100%"
                filterable
              >
                <el-option
                  v-for="item in proxy.addListNoLimit(sys_instrument_step)"
                  :key="item.value"
                  :label="item.label"
                  :value="parseInt(item.value)"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="核查日期：">
              <el-date-picker
                v-model="dateRange1"
                value-format="YYYY-MM-DD"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="下次检定日期：">
              <el-date-picker
                v-model="dateRange2"
                value-format="YYYY-MM-DD"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <!-- 字段 -->
            <el-form-item label="核查人：" prop="createUserName">
              <el-input
                clearable
                maxlength="100"
                v-model="searchOutinForm.createUserName"
                @keyup.enter="getcheckListLedger"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8"> </el-col>
          <el-col :span="8">
            <el-row justify="end">
              <el-button type="success" @click="getcheckListLedger"
                >搜索</el-button
              >
              <el-button class="resetBtn" @click="resetBtn">重置</el-button>
            </el-row>
          </el-col>
        </el-row>
      </el-form>
      <!-- /搜索条件区域 -->

      <!-- 技术指标表格 -->
      <el-row style="justify-content: space-between">
        <el-row>
          <p class="pinformation">期间核查记录</p>
          <p class="totalStyle">共 {{ OtinTotal }} 条数据</p>
        </el-row>
      </el-row>

      <el-table
        ref="sub1TableRef"
        class="business"
        v-loading="targetTableLoading"
        :data="targetList"
        @selection-change="outSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <!-- 字段 -->
        <el-table-column label="核查日期" align="center" prop="checkDate" />
        <el-table-column label="核查人" align="center" prop="createUserId">
          <template #default="scope">
            {{
              proxy.getItemByArray(
                userList,
                "userId",
                scope.row.createUserId,
                "nickName"
              )
            }}
          </template>
        </el-table-column>
        <el-table-column label="质量负责人" align="center" prop="" />
        <el-table-column
          label="下次核查日期"
          align="center"
          prop="checkNextDate"
        />
        <el-table-column label="状态" align="center" prop="stepId">
          <template #default="scope">
            <dict-tag
              :options="sys_instrument_step"
              :value="scope.row.stepId"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-button link type="success">查看</el-button>
            <el-button link type="success">编辑</el-button>
            <el-button link type="success" @click="delCheckClick(scope.row)"
              >删除</el-button
            >
            <el-button link type="success">导出记录</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- /技术指标表格 -->
      <pagination
        v-show="OtinTotal > 0"
        :total="OtinTotal"
        v-model:page="searchOutinForm.pageNum"
        v-model:limit="searchOutinForm.pageSize"
      />
      <!-- @pagination="" -->
    </el-card>
  </div>
</template>

<script setup name="Instrument">
import {
  listLedger,
  FormLedger,
  checkListLedger,
  delLedger,
} from "@/api/resource/ledger";
import { listUser } from "@/api/contract/check";

const { proxy } = getCurrentInstance();
const { sys_instrument_step, sys_bzwz_gslb } = proxy.useDict(
  "sys_instrument_step",
  "sys_bzwz_gslb"
);
const loading = ref(true);
const ids = ref([]);
const total = ref(0);
const bzwzTable = ref([]);
const targetTableLoading = ref(false);
const dateRange = ref([]);
const dateRange1 = ref([]);
const dateRange2 = ref([]);
const dateRange3 = ref([]);
const searchOutinForm = ref({
  pageNum: 1,
  pageSize: 10,
  stepId: -1
});
onMounted(() => {
  getList();
  nextTick(() => {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  })
})
onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});

onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [
        { tableName: "main", bottomHeight: 73 },
        { tableName: "sub1", bottomHeight: 73 },
      ];
    } else {
      heightParams.value = [
        { tableName: "main", bottomHeight: 21 },
        { tableName: "sub1", bottomHeight: 73 },
      ];
    }
    getTableHeight();
  }
);

const heightParams = ref([
  { tableName: "main", bottomHeight: 21 },
  { tableName: "sub1", bottomHeight: 21 },
]);

function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    gsType: -1,

  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询仪器档案列表 */
function getList() {
  loading.value = true;
  listLedger(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (response) => {
      bzwzTable.value = response.rows;
      total.value = response.total;
      loading.value = false;
    }
  );
}

// 表单重置
function reset() {
  form.value = {};
  proxy.resetForm("");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  dateRange.value = [];
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.instrumentId);
}

const outIds = ref([]);
function outSelectionChange(selection) {
  outIds.value = selection.map((item) => item.outinId);
}

const OtinTotal = ref(0);

const targetList = ref([]);

function resetBtn() {
  proxy.resetForm("searchOutinRef");
  dateRange1.value = [];
  dateRange2.value = [];
  getcheckListLedger();
}

function rowClick(row) {
  proxy.resetForm("searchOutinRef");
  dateRange1.value = [];
  dateRange2.value = [];
  FormLedger(row.bzwzId).then((res) => {
    searchOutinForm.value.bzwzCode = res.data.bzwzCode;
    searchOutinForm.value.bzwzName = res.data.bzwzName;
    searchOutinForm.value.produceCode = res.data.produceCode;
    searchOutinForm.value.bzwzId = res.data.bzwzId;
    heightBright.value = row.bzwzId;
    getcheckListLedger();
  });
}

function getcheckListLedger() {
  if (searchOutinForm.value.bzwzId) {
    searchOutinForm.value.params = {
      beginTime: dateRange1.value[0],
      endTime: dateRange1.value[1],
      beginTime1: dateRange2.value[0],
      endTime1: dateRange2.value[1],
    };
    checkListLedger(searchOutinForm.value).then((res) => {
      targetList.value = res.rows;
      OtinTotal.value = res.total;
    });
  } else {
    proxy.$modal.msgError("请先选择要搜索的仪器设备");
  }
}

function delCheckClick(row) {
  proxy.$modal
    .confirm("是否确认删除该条记录？")
    .then(function () {
      delLedger(row.checkId).then((res) => {
        proxy.$modal.msgSuccess("删除成功");
        getcheckListLedger();
      });
    })
    .catch(() => {});
}

const heightBright = ref("");
function tableRowClass({ row }) {
  if (row) {
    if (row.bzwzId == heightBright.value) {
      return "current-row";
    }
    return "";
  }
}

const userList = ref([]);
listUser().then((res) => {
  userList.value = res.data;
});
</script>

<style lang="scss" scoped></style>
